package com.zy.config;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;
import java.util.Map;

@Component
public class QwenPlusEmbeddingClient {

    private final WebClient webClient;

    public QwenPlusEmbeddingClient() {
        // 直接写死千问 embedding 地址和 API Key
        String baseUrl = "https://dashscope.aliyuncs.com/v1";
        String apiKey = "sk-b5f1fafe19774d12966dd3b7144ba8cc";

        this.webClient = WebClient.builder()
                .baseUrl(baseUrl)
                .defaultHeader("Authorization", "Bearer " + apiKey)
                .build();
    }

    public List<Double> getEmbedding(String text) {
        Map<String, Object> request = Map.of(
                "model", "qwen-plus",
                "input", text
        );

        Map<String, Object> response = webClient.post()
                .uri("/embeddings") // 千问 embedding 接口
                .bodyValue(request)
                .retrieve()
                .bodyToMono(Map.class)
                .block();

        // 假设返回 JSON 是 { "data": [{"embedding": [...] }] }
        List<Map<String, Object>> data = (List<Map<String, Object>>) response.get("data");
        return (List<Double>) data.get(0).get("embedding");
    }
}
